package com.dwd.pay.service.impl;

import com.dwd.basic.constant.PayConstants;
import com.dwd.basic.service.impl.BaseServiceImpl;
import com.dwd.order.mapper.ProductOrderMapper;
import com.dwd.order.pojo.ProductOrder;
import com.dwd.pay.mapper.PayBillMapper;
import com.dwd.pay.pojo.PayBill;
import com.dwd.pay.service.IPayBillService;
import com.dwd.quartz.service.IQuartzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/28 16:09
 * @Version 1.0
 */
@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private IQuartzService quartzService;

    /**
     * 修改状态和时间
     *
     * @param out_trade_no 商户订单号
     * @param trade_no     支付宝交易号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePayBill(String out_trade_no, String trade_no) {
        //根据订单号查询paybill对象
        PayBill payBill = payBillMapper.queryByorderSn(out_trade_no);
        //设置完成时间 支付宝单号  状态
        payBill.setUpdateTime(new Date())
                .setUnionPaySn(trade_no)
                .setState(PayConstants.PAY_COMPLETE);
        //修改
        payBillMapper.update(payBill);
        switch (payBill.getBusinessType()) {
            //产品
            case PayConstants.BUSINESSTYPE_PRODUCT:
                //查询对应的产品订单
                ProductOrder productOrder = productOrderMapper.queryById(payBill.getBusinessKey());
                //设置状态
                productOrder.setState(PayConstants.PAY_COMPLETE)
                        .setPaySn(trade_no)
                        .setLastConfirmTime(new Date());
                //修改产品订单表
                productOrderMapper.update(productOrder);
                //取消定时器
                quartzService.remove(productOrder);

                break;
            //宠物领养
            case PayConstants.BUSINESSTYPE_ADOPT:
                break;
            //商品
            case PayConstants.BUSINESSTYPE_GOODS:
                break;
            default:
        }
    }
}
